package com.example.photograph.controller;

import com.example.photograph.domain.AjaxResult;
import com.example.photograph.domain.SysOrderItem;
import com.example.photograph.domain.SysUser;
import com.example.photograph.page.TableDataInfo;
import com.example.photograph.service.ISysOrderItemService;
import com.example.photograph.service.ISysUserService;
import com.example.photograph.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单详情Controller
 * 
 * @author wuyj
 * @date 2021-03-06
 */
@Controller
@RequestMapping("/photograph/item")
public class SysOrderItemController extends BaseController
{
    private String prefix = "photograph/item";

    @Autowired
    private ISysOrderItemService sysOrderItemService;
    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("photograph:item:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询订单详情列表
     */
    @RequiresPermissions("photograph:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysOrderItem sysOrderItem)
    {
        startPage();
        Long userId = ShiroUtils.getUserId();
        SysUser user = sysUserService.selectUserById(userId);
        if ("client".equals(user.getRole().getRoleKey())){
            sysOrderItem.setClientId(userId);
        }
        List<SysOrderItem> list = sysOrderItemService.selectSysOrderItemList(sysOrderItem);
        return getDataTable(list);
    }

    /**
     * 新增订单详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存订单详情
     */
    @RequiresPermissions("photograph:item:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysOrderItem sysOrderItem)
    {
        return toAjax(sysOrderItemService.insertSysOrderItem(sysOrderItem));
    }

    /**
     * 修改订单详情
     */
    @GetMapping("/edit/{orderItemId}")
    public String edit(@PathVariable("orderItemId") Long orderItemId, ModelMap mmap)
    {
        SysOrderItem sysOrderItem = sysOrderItemService.selectSysOrderItemById(orderItemId);
        mmap.put("sysOrderItem", sysOrderItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单详情
     */
    @RequiresPermissions("photograph:item:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysOrderItem sysOrderItem)
    {
        return toAjax(sysOrderItemService.updateSysOrderItem(sysOrderItem));
    }

    /**
     * 删除订单详情
     */
    @RequiresPermissions("photograph:item:remove")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysOrderItemService.deleteSysOrderItemByIds(ids));
    }
}
